/*
 * CS_REQ_ZEN_SHOP_ITEM_LIST.cpp
 *
 *  Created on: 2013. 5. 28.
 *      Author: the2
 */

#include "../../Command.h"
#include "../../NResource/Static.h"
#include "../../User.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_REQ_ZEN_SHOP_ITEM_LIST(User * pUser, ::Network::Packet * pPacket)
{
/*	ZNO_CS_REQ_ZEN_SHOP_ITEM_LIST		0x2306
	{
	     u8     u8category;     // 0: 아바타, 1:탈것, 2:보물함, 3:기타
	}

	ZNO_SC_REQ_ZEN_SHOP_ITEM_LIST		0x2307
	{
		s16	s16ack;
	     u8     u8category;
	     u8     u8count;
	     {
	          u16     u16seq;
			  u8	u8item_icon; // 추가 2013-07-02
	          u8     u8sheet;
	          u16     u16tid;
	          u16     u16price;
	          u8     u8discount_percentage;
	          u32     u32item_count;
	          u8     u8position;
	          str strdescription;
	     }[u8count];
	}*/

	PREPARE_PACKET;

	uint8_t itemCategory = 0;	// 0:아바타 1:탈것 2:보물함 3:기타

	*pPacket >> itemCategory;


	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	NResource::ChargeItemData * pCharge = NResource::Static::InstancePtr()->GetChargeItem()->GetDataPtr(pUser);

	if (pCharge == NULL)
	{
		DEBUGLOG("CS_REQ_ZEN_SHOP_ITEM_LIST : pCharge is NULL!!!");

		*pAck << static_cast<int16_t>(ACK_UNKNOWN_ERROR);
		*pAck << itemCategory;
		*pAck << static_cast<char>(0);
	}
	else
	{
		NResource::ChargeItemData::CHARGEMAP & clsChargeMap = pCharge->GetTree();

		auto citer = clsChargeMap.begin();

		*pAck << static_cast<int16_t>(ACK_OK);
		*pAck << itemCategory;
		*pAck << static_cast<char>(pCharge->size(itemCategory, pUser->GetCharacterInfo().classtype));

		for ( ; citer != clsChargeMap.end(); citer++)
		{
			if (citer->second->_CATEGORY != itemCategory)
				continue;

			if (citer->second->_POSITION < 0)
				continue;

			if (citer->second->_CLASSTYPE >= 0
					&& citer->second->_CLASSTYPE != static_cast<int>(pUser->GetCharacterInfo().classtype) )
				continue;

			*pAck << static_cast<uint16_t>(citer->second->_SEQ);
			*pAck << static_cast<char>(citer->second->_ICON);
			*pAck << static_cast<char>(citer->second->_SHEET);
			*pAck << static_cast<uint16_t>(citer->second->_TID);
			*pAck << static_cast<uint16_t>(citer->second->_PRICE);
			*pAck << static_cast<char>(citer->second->_DISCOUNT);
			*pAck << citer->second->_QUANTITY;
			*pAck << static_cast<char>(citer->second->_POSITION);
			*pAck << citer->second->_EXPLAIN.c_str();

			DEBUGLOG("CS_REQ_ZEN_SHOP_ITEM_LIST : category=%d, seq=%d", itemCategory, citer->second->_SEQ);
		}
	}

	pAck->MakePacket(CMD_SC_REQ_ZEN_SHOP_ITEM_LIST);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/
